# encoding: utf-8
from flask import Flask, request
# 可以做ip黑名单 environ 里面有ip
app = Flask(__name__)


@app.route('/')
def index():
    return "hello world"


class Middleware(object):
    def __init__(self, old_wsgi_app):
        """
        当服务端启动时候
        """
        from flask import session
        print("dayin session",session)
        self.old_wsgi_app = old_wsgi_app

    def __call__(self, environ, start_response):
        """
        当每次启动的时候
        environ 里面有原始请求 包括ip
        """
        obj = self.old_wsgi_app( environ, start_response)
        return obj


if __name__ == '__main__':
    app.wsgi_app = Middleware(app.wsgi_app)
    app.run(debug=True)
    """
    1.当请求到来的时候调用 app.__call__方法
    2.再调用app.wsgi_app方法
    """
